<script>
import { mapMutations, mapActions, mapState } from 'vuex';
import wechat from '@/shopro/wechat/wechat';

export default {
	globalData: {},
	onLaunch(options) {
		let that = this;
		// #ifdef H5
		that.$platform.entry();
		// #endif
		// #ifdef MP-WEIXIN
		// 检测小程序更新(如果从朋友圈场景进入则无此API)
		options.scene !== 1154 && wechat.checkMiniProgramUpdate();
		// #endif
		// #ifdef APP-PLUS
		if(plus.os.name=="Android"){
			that.$http('common.checkVersion', {appid:plus.runtime.appid,version:plus.runtime.version}).then(res => {
				if (res.code === 1) {
					uni.showModal({
						title: "更新提示",
						content: res.msg,
						success: (ret) => {  
							if (ret.confirm) {								
								//plus.runtime.openURL(res.android_url);
								var dtask = plus.downloader.createDownload(res.android_url, {}, function (d, status ) {  
									// 下载完成  
									if ( status == 200 ) {   
										plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename),{},{},function(error){  
											uni.showToast({  
												title: '安装失败', 
												duration: 1500  
											});  
										})
									} else {  
										 uni.showToast({  
											title: '更新失败',
											duration: 1500  
										 });  
									}    
								});  
								dtask.start();
								var prg = 0;
								var showLoading = plus.nativeUI.showWaiting("正在下载");
								dtask.addEventListener('statechanged', function(task,status) {
									// 给下载任务设置一个监听 并根据状态 做操作
									switch (task.state) {
										case 1:
										showLoading.setTitle("正在下载");
										break;
										case 2:
										showLoading.setTitle("已连接到服务器");
										break;
										case 3:
										prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100);
										showLoading.setTitle("版本更新,正在下载" + prg + "% ");
										break;
										case 4:
										plus.nativeUI.closeWaiting();
										//下载完成
										break;
									}
								});								
								//plus.runtime.openURL(res.ios_url);																
							} else {
								plus.runtime.quit();  
							}
						}  
					})
				}
			});
		}
		// #endif
		that.appInit(options);
		if (process.env.NODE_ENV === 'development') {
			this.syncPages();
		}
	},
	methods: {
		//应用初始化,获取模板数据，自动同步新页面到后台,获取用户信息
		...mapActions('shopro',['appInit', 'syncPages'])
	}
};
</script>

<style lang="scss">
@import 'uview-ui/index.scss';
@import 'static/font/shopro-icon.css';
@import "/common/iconfont/iconfont.css";

/* 字体文件 */
@font-face {
	/*font-family: OPPOSANS;
	src: url('~@/static/font/OPPOSANS-M-subfont.ttf');*/
}
.font-OPPOSANS {
	/*font-family: OPPOSANS;*/
}
page {
	-webkit-overflow-scrolling: touch; // ios滑动不流畅
	height: 100%;
	background: #f6f6f6;
	width: 100%;
	font-size: 30rpx;
	/*font-family: OPPOSANS;*/
	word-break: break-all; //英文文本不换行
	color: $u-main-color;
}
::-webkit-scrollbar {
	width: 0;
	height: 0;
	color: transparent;
	display: none;
}

</style>
